<template>
  <van-loading v-show="!viewLoad"  type="spinner" color="#1989fa" />
  <van-space v-show="viewLoad" direction="vertical" fill :size="50">

    <van-image :src="ttlong" />
    <van-form @submit="onSubmit">
      <van-cell-group inset>
        <van-field
            v-model="username"
            input-align="center"
            name="username"
            placeholder="用户名"
            :rules="[{ required: true, message: '请填写用户名' }]"
        />
        <van-field
            v-model="password"
            input-align="center"
            type="password"
            name="password"
            placeholder="密码"
            :rules="[{ required: true, message: '请填写密码' }]"
        />
      </van-cell-group>
      <van-row justify="space-around">
        <van-col span="8"> <van-button
            plain hairline native-type="submit"
            loading-text="登陆中..."
            :loading="load"
            type="primary">确认登录</van-button></van-col>
        <van-col span="8"><van-button plain hairline type="success" @click="fogetPS">忘记密码</van-button></van-col>
      </van-row>
    </van-form>
  </van-space>
</template>

<script setup>
import ttlong from  "@/assets/back.png"
import {ref, getCurrentInstance, onBeforeMount} from "vue";
import {get_user, login} from "@/axios/login";
import {useRouter,useRoute} from 'vue-router'
import {tokenStore} from "@/stores/TokenStore.js"
import {userStore} from "@/stores/UserStore.js"

const router = useRouter();
const route = useRoute();
const urlll = route.query.urlll
const tokenU = tokenStore()
const userS = userStore();
const viewLoad = ref(false);



const load = ref(false);
const username = ref('');
const password = ref('');

onBeforeMount(() => {

  get_user().then(res=>{
    console.log(res.data.data);
    userS.setUser(res.data.data);
    if(urlll){
      router.push(urlll)
    }else {
      router.push("/")
    }
  }).catch(e=>{
    viewLoad.value = true
  })
})


const onSubmit = (values) => {
  load.value = true;
  login(values).then(res=>{
    tokenU.setToken(res.data.access_token)
    tokenU.setRefreshToken(res.data.refresh_token)
    get_user().then(res=>{
      userS.setUser(res.data.data);
      if(urlll){
        router.push(urlll)
      }else {
        router.push("/")
      }
    })
  }).then(()=>{
    load.value  = false;
  }).catch(err=>{
    load.value  = false;
  })
}

const fogetPS = () =>{
    router.push("/changePassword")
}



</script>

<style scoped>
*{
  background-color: #FFFFFF;

}

</style>